import React, {Component} from 'react';
import {Text, View, ScrollView, Alert} from 'react-native';
import {Toast} from '@ant-design/react-native';
import {getParkingPlanPackageByItemID, judgeParkingState} from '~/api/parking';
import {connect} from 'react-redux';
import {getOwnerHouseBuilding} from '~/api/owner';
import SelectOwnerHouse from '~/components/parkinfo/selectownerhouse';
import {saveSystem} from '~/store/system';
import {initParkOrder, saveParkOrder} from '~/store/parkorder';
import Parkpublics from '~/components/process/parkpublics';
import Getgift from '~/components/process/getgift';
import GetGiftInfo from '~/pages/developer/info/community/GetGiftInfo';

const mapGetter = (state) => {
    return {
        userinfo: state.userinfo,
        system: state.system,
        parkorder: state.parkorder,
    };
};

const mapSetter = (dispatch) => {
    return {
        onSave: (data) => {
            dispatch(saveSystem(data));
        },
        onparkSave: (data) => {
            dispatch(saveParkOrder(data));
        },
        initParkOrder: () => {
            dispatch(initParkOrder());
        },
    };
};
class Gift extends Component {
    static navigationOptions = {
        title: '套餐列表',
    };
    constructor(props) {
        //调用基类的所有的初始化方法
        super(props);

        // 设置当前组件的属性
        this.state = {
            parkinginfo: this.props.navigation.getParam('parkinginfo'),
            parkingplanitem: this.props.navigation.getParam('parkingplanitem'),
            ownerhouseid: this.props.parkorder.ownerhouseid || 0,
            packagelist: [],
            houselist: [],
            houseselectvisible: false,
            stop: true,
            selectowner: false,
        };
        this.seleonPress = () => {
            if (this.state.ownerhouseid === 0) {
                getOwnerHouseBuilding(this.state.parkinginfo.CompanyBuildingID).then((r) => {
                    if (r.code === 0) {
                        if (r.data === '') {
                            this.props.navigation.push('Information', {
                                parkinginfo: this.state.parkinginfo,
                            });
                        } else {
                            this.setState(
                                {
                                    houseselectvisible: true,
                                },
                                () => {
                                    this.setState({
                                        selectowner: true,
                                    });
                                },
                            );
                        }
                    } else {
                        Toast.info(r.msg);
                    }
                });
            } else {
                this.setState({stop: true});
                this.props.navigation.push('Process');
            }
        };
        this.onClose = () => {
            this.setState({
                houseselectvisible: false,
            });
        };
        getParkingPlanPackageByItemID(this.state.parkinginfo.ParkingPlanItemID).then((r) => {
            if (r.code === 0) {
                this.setState({
                    packagelist: r.data,
                });
            } else {
                Toast.info(r.msg);
            }
        });
        console.disableYellowBox = true;
    }
    confirmorder = (confirmorder) => {
        if (confirmorder > 0) {
            this.setState({stop: true});
            judgeParkingState(this.props.parkorder.parkingid).then((r) => {
                if (r.code === 0) {
                    this.onClose();
                    this.props.onparkSave({
                        parkingid: this.state.parkinginfo.ParkingDataID,
                        ownerhouseid: confirmorder,
                        parkingplanid: this.state.parkingplanitem.ParkingPlanID,
                        parkingplanitemid: this.state.parkingplanitem.ParkingPlanItemID,
                        projectid: this.state.parkinginfo.ProjectDataID,
                        companybuildingid: this.state.parkinginfo.CompanyBuildingID,
                        parkinginfo: this.state.parkinginfo,
                        parkingplanitem: this.state.parkingplanitem,
                    });
                    this.props.navigation.push('Process');
                } else {
                    Toast.info(r.msg);
                    this.props.navigation.push('ChangePark');
                }
            });
        } else {
            Alert.alert('请选择个人信息');
        }
    };
    render() {
        return (
            <View style={{flex: 1}}>
                <ScrollView style={{flex: 1}}>
                    <Parkpublics parkinginfo={this.state.parkinginfo} />
                    <Getgift parkingitemid={this.state.parkingplanitem.ParkingPlanItemID} navigation={this.props.navigation} packageradio={1} />
                    <GetGiftInfo
                        paused={this.state.stop}
                        updateState={(state) => {
                            this.setState({stop: state});
                        }}
                    />
                </ScrollView>
                {this.props.userinfo.identity === 8 && (
                    <View
                        style={{
                            width: '100%',
                            height: 49,
                            backgroundColor: '#EC4F46',
                            borderColor: '#EC4F46',
                            borderWidth: 1,
                        }}>
                        {this.props.userinfo.token === '' ? (
                            <Text
                                style={{
                                    fontSize: 15,
                                    color: 'white',
                                    textAlign: 'center',
                                    paddingTop: 10,
                                }}
                                onPress={() => {
                                    this.props.navigation.push('Login');
                                }}>
                                我要购买
                            </Text>
                        ) : (
                            <Text
                                style={{
                                    color: 'white',
                                    fontSize: 15,
                                    textAlign: 'center',
                                    paddingTop: 10,
                                }}
                                onPress={this.seleonPress}>
                                我要购买
                            </Text>
                        )}
                        {this.state.selectowner && (
                            <SelectOwnerHouse
                                parkinginfo={this.state.parkinginfo}
                                parkingplanitem={this.state.parkingplanitem}
                                ownerdataid={this.state.ownerdataid}
                                houseselectvisible={this.state.houseselectvisible}
                                confirmorder={(ownerdataid) => {
                                    this.confirmorder(ownerdataid);
                                }}
                                onparkSave={this.props.onparkSave}
                                onClose={this.onClose}
                                navigation={this.props.navigation}
                            />
                        )}
                    </View>
                )}
            </View>
        );
    }
}

export default connect(mapGetter, mapSetter)(Gift);
